package com.example.qxfw.hbqx.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.qxfw.common.entity.zy.SumAtmosphericDayData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
@DS("service")
public interface SumAtmosphericDayDataMapper extends BaseMapper<SumAtmosphericDayData> {

    /**
     *根据时间段查询空气逐小时实况空气质量日统计表
     * @param observtimeStart 起始时间
     * @param observtimeEnd 终止时间
     * @return
     */
    @Select("SELECT stationid, observtime, longitude, latitude, altitude, stationname, aqi, " +
            "firstpollution, pollutionlevel, concenpm2p5, fractpm2p5, concenpm10, fractpm10, concenco, " +
            "fractco, conceno3, fracto3, conceno38h, fracto38h, concenso2, fractso2, concenno2, fractno2 " +
            "FROM sum_atmospheric_day_data " +
            "WHERE observtime >= #{observtimeStart}  AND observtime <= #{observtimeEnd} " +
            "ORDER BY observTime DESC " +
            "LIMIT 1 ")
    List<SumAtmosphericDayData> selectOrderByObservtimePeriodOfSumAtmosphericDayData(Date observtimeStart, Date observtimeEnd);

    /**
     * 根据站点和时间段查询空气质量日统计表
     * @param observtimeStart 起始时间
     * @param observtimeEnd 终止时间
     * @param stationid 站点编号
     * @return
     */
    @Select("SELECT observtime, aqi, fractpm2p5, fractpm10, fractco, fracto3, fracto38h, fractso2, fractno2 " +
            "FROM sum_atmospheric_day_data " +
            "WHERE observtime >= #{observtimeStart}  AND observtime <= #{observtimeEnd} AND stationid = #{stationid} " +
            "ORDER BY observtime ")
    List<SumAtmosphericDayData> selectByObservtimeAndstationid(Date observtimeStart, Date observtimeEnd, String stationid);

    /**
     * 根据站点和开始时间查询空气质量日统计表
     * @param observtimeStart 开始时间
     * @return
     */
    @Select("SELECT observtime, aqi, fractpm2p5, fractpm10, fractco, fracto3, fracto38h, fractso2, fractno2 " +
            "FROM sum_atmospheric_day_data " +
            "WHERE observtime = #{observtimeStart} " +
            "ORDER BY observtime")
    List<SumAtmosphericDayData> selectByTimeOfSumAtmosphericDayData(Date observtimeStart);

    /**
     * 除了stationid以外的站点的观测时间
     * @param stationid 站点id
     * @return
     */
    @Select("SELECT DISTINCT observtime " +
            "FROM sum_atmospheric_day_data " +
            "WHERE stationid NOT LIKE concat('%',#{stationid},'%') " +
            "ORDER BY observtime DESC " +
            "LIMIT 3 ")
    List<SumAtmosphericDayData> getObservtimeByNotStationId(String stationid);

    /**
     * 获取在规定时间段中非stationid的观测时间,升序
     * @param observtimeStart 观测时间起点
     * @param observtimeEnd 观测时间终点
     * @param stationid 站点id
     * @return
     */
    @Select("SELECT DISTINCT observtime " +
            "FROM sum_atmospheric_day_data " +
            "WHERE observtime >= #{observtimeStart} AND observtime <= #{observtimeEnd} AND stationid NOT LIKE concat('%',#{stationid},'%') " +
            "ORDER BY observtime ASC ")
    List<SumAtmosphericDayData> getByObservtimeAndNotStationid(Date observtimeStart, Date observtimeEnd, String stationid);


    /**
     * 根据元素和时间段获取空气质量日统计表信息
     * @param observtimeStart 开始时间
     * @param observtimeEnd 结束时间
     * @param element 字段
     * @return
     */
    @Select("SELECT stationid, observtime, longitude, latitude, altitude, stationname, ${element} " +
            "FROM sum_atmospheric_day_data " +
            "WHERE observtime >= #{observtimeStart} AND observtime <= #{observtimeEnd} ")
    List<SumAtmosphericDayData> getByTimePeriodAndElementOfSumAtmosphericDayData(Date observtimeStart, Date observtimeEnd, String element);


    /**
     * 根据元素和时间段获取空气质量日统计表信息
     * @param observtime 开始时间
     * @param element 字段
     * @return
     */
    @Select("SELECT stationid, observtime, longitude, latitude, altitude, stationname, ${element} " +
            "FROM sum_atmospheric_day_data " +
            "WHERE observtime = #{observtime} ")
    List<SumAtmosphericDayData> getByTimeAndElementOfSumAtmosphericDayData(Date observtime, String element);

    @Select("SELECT aqi " +
            "FROM table_name " +
            "WHERE observTime = #{observtime} and StationID = #{stationid} " +
            "ORDER BY observTime; ")
    List<SumAtmosphericDayData> selectByTimeAndIdOfSumAtmosphericDayData(Date observtime, String stationid);
}
